package XMLSerialization;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.thoughtworks.xstream.XStream;

public class XMLServer {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		XStream xstream = new XStream();
	    ServerSocket s = new ServerSocket(90);
	    Socket clientSocket = s.accept();
	    System.out.println("Client was accepted");
	    InputStream in = clientSocket.getInputStream();
    	OutputStream out = clientSocket.getOutputStream();
	    OutputStream outFile = new FileOutputStream(args[0]);
	    SensorData obj;
	    do {
	        obj = (SensorData)xstream.fromXML(in);
	        if (obj != null) {
	        	xstream.toXML(obj, outFile);
	        	xstream.toXML("end", out);
	        	out.flush();
	        }
	    } while (obj != null);
	    outFile.close();
	    s.close();
	}
}
